#include "udpapp.h"
#include "ui_udpapp.h"

UdpApp::UdpApp(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UdpApp)
{
    ui->setupUi(this);

    //关联读数据信号
    connect(&msocket,&QUdpSocket::readyRead,this,&UdpApp::get_ip);

    //发送广播到9999端口
    QByteArray array("getip");

    //QHostAddress::Broadcast 在有些路由器使用有问题 用ip代替
    msocket.writeDatagram(array,QHostAddress::Broadcast,9999);


}

UdpApp::~UdpApp()
{
    delete ui;
}

void UdpApp::get_ip()
{
    QHostAddress ctladdr;
    quint16 port;
    QByteArray array;  //要另外分配空间
    array.resize(100);   //分配空间
    msocket.readDatagram(array.data(),array.size(),&ctladdr,&port);
    QString ip = ctladdr.toString();
    ip.remove("::ffff:");
    ui->lineEdit->setText(ip);
}

void UdpApp::on_searchBt_clicked()
{
    //发送广播到9999端口
    QByteArray array("getip");

    //QHostAddress::Broadcast 在有些路由器使用有问题 用ip代替
    msocket.writeDatagram(array,QHostAddress::Broadcast,9999);
}

















